//
//  TicketHomeViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-30.
//
//

#import "TicketHomeViewController.h"
#import "DeviceUtil.h"

#import "MovieView.h"
#import "SiteView.h"
#import "ArtView.h"

#import "MovieDetailViewController.h"
#import "SiteDetailViewController.h"
#import "ArtDetailViewController.h"

@interface TicketHomeViewController ()

@end

@implementation TicketHomeViewController
{
    UISegmentedControl *_segment;
    
    MovieView *_movieView;
    SiteView *_siteView;
    ArtView *_artView;
    
    NSInteger _segFlag;
}

- (void)dealloc
{
    [_segment release];
    [_movieView release];
    [_siteView release];
    [_artView release];
    
    _segment = nil;
    _movieView = nil;
    _siteView = nil;
    _artView = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//初始化视图
- (void)init_view
{
    [self.navigationItem setTitle:@"票务"];
    
    [self initMovieView];
    [self initSiteView];
    [self initArtView];
    [self initSegmentCtrl];
    
    [self addMovieView];
}

//初始化SegmentCtrl
- (void)initSegmentCtrl
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, height, [DeviceUtil getScreenWidth], 30)];
    [_segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [_segment setBackgroundColor:[UIColor lightGrayColor]];
    [_segment insertSegmentWithTitle:@"电影票" atIndex:0 animated:NO];
    [_segment insertSegmentWithTitle:@"景点票" atIndex:1 animated:NO];
    [_segment insertSegmentWithTitle:@"演艺票" atIndex:2 animated:NO];
    
    [_segment addTarget:self action:@selector(onTabSelected:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_segment];
}

-(void)onTabSelected:(id)sender
{
    UISegmentedControl *control = (UISegmentedControl *)sender;
    
    switch (control.selectedSegmentIndex) {
        case 0:
            [self addMovieView];
            [self removeSiteView];
            [self removeArtView];
            break;
        case 1:
            [self removeMovieView];
            [self addSiteView];
            [self removeArtView];
            break;
        case 2:
            [self removeMovieView];
            [self removeSiteView];
            [self addArtView];
            break;
        default:
            break;
    }
    
}

//电影票
- (void)initMovieView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _movieView = [[MovieView alloc] initWithFrame:CGRectMake(0, height+30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height-55) andDelegate:self
                                    coverDelegate:self];
}
- (void)addMovieView
{
    if (_movieView)
    {
        _segFlag = 0;
        [self.view addSubview:_movieView];
    }
}
- (void)removeMovieView
{
    if (_movieView)
    {
        [_movieView removeFromSuperview];
    }
}

//景点票
- (void)initSiteView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _siteView = [[SiteView alloc] initWithFrame:CGRectMake(0, height+30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height-55) andDelegate:self typeDelegate:self];
}
- (void)addSiteView
{
    if (_siteView)
    {
        _segFlag = 1;
        [self.view addSubview:_siteView];
    }
}
- (void)removeSiteView
{
    if (_siteView)
    {
        [_siteView removeFromSuperview];
    }
}

//演艺票
- (void)initArtView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _artView = [[ArtView alloc] initWithFrame:CGRectMake(0, height+30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height-55) andDelegate:self typeDelegate:self];
}
- (void)addArtView
{
    if (_artView)
    {
        _segFlag = 2;
        [self.view addSubview:_artView];
    }
}
- (void)removeArtView
{
    if (_artView)
    {
        [_artView removeFromSuperview];
    }
}


#pragma mark movieview delegate
//查看电影票详情
-(void)detailClick:(MovieInfo*)movieinfo
{
    MovieDetailViewController *detailController = [[MovieDetailViewController alloc] init];
    detailController.movieInfo = movieinfo;
    [self.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}
//查看电影票详情
-(void)coverDetailClick:(MovieInfo*)movieinfo
{
    MovieDetailViewController *detailController = [[MovieDetailViewController alloc] init];
    detailController.movieInfo = movieinfo;
    [self.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}

#pragma mark paysite delegate
////查看景点票详情
//-(void)clickPaySiteView:(AllSiteInfo*)siteInfo
//{
//    SiteDetailViewController *siteController = [[SiteDetailViewController alloc] init];
//    siteController.allSiteInfo = siteInfo;
//    [self.navigationController pushViewController:siteController animated:YES];
//    [siteController release];
//}
#pragma mark typesite delegate
//查看景点票详情
-(void)clickTypeSiteView:(AllSiteInfo*)siteInfo siteTicketInfo:(NSMutableArray*)siteTicketInfo
{
    SiteDetailViewController *siteController = [[SiteDetailViewController alloc] init];
    siteController.allSiteInfo = siteInfo;
    siteController.siteTicketInfo = siteTicketInfo;
    [self.navigationController pushViewController:siteController animated:YES];
    [siteController release];
}

#pragma mark paysite delegate
////查看演艺票详情
//-(void)clickArtTimeView:(NSInteger)index
//{
//    ArtDetailViewController *artController = [[ArtDetailViewController alloc] init];
//    [self.navigationController pushViewController:artController animated:YES];
//    [artController release];
//}
#pragma mark typesite delegate
//查看演艺票详情
-(void)clickArtTypeView:(ArtInfo*)artInfo
{
    ArtDetailViewController *artController = [[ArtDetailViewController alloc] init];
    artController.artInfo = artInfo;
    [self.navigationController pushViewController:artController animated:YES];
    [artController release];
}

@end




